package Object;

import Util.HttpUtil;
import java.io.File;
import java.io.IOException;

/**
 * @author jklu
 * ????? ---> ??????????е????????????
 */
public class MyRunnable implements Runnable {
    private Pic pic;
    private int spider;
    private boolean useProxy;

    public Pic getPic() {
        return pic;
    }

    public void setPic(Pic pic) {
        this.pic = pic;
    }

    public boolean isUseProxy() {
        return useProxy;
    }

    public MyRunnable(Pic pic, int spider, boolean useProxy){
        this.pic = pic;
        this.spider = spider;
        this.useProxy = useProxy;
    }
    @Override
    public void run() {
        boolean isStop = false;
        //?????????
        String url = pic.getUrl();
        //??????
        String parentPath = "";
        //?????????
        String format = pic.getType();
        switch (spider){
            case 1:
                parentPath = Constant.SAVE_PATH_BAIDU;
                break;
            case 2:
                parentPath = Constant.SAVE_PATH_BAIDU2;
                break;
            case 3:
                parentPath = Constant.SAVE_PATH_GOOGLE;
                break;
            case 4:
                parentPath = Constant.SAVE_PATH_BING;
                break;
            default:
                break;
        }
        //??????????????
        File f = new File(parentPath,pic.getTitle().replace(" ","")+"."+format);
        //?ж??????????--->????????????--->????
        if(!f.exists()) {
            try {
                f.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else{
            System.out.println("File has existed !!!");
            isStop = true;
        }
        if(!isStop) {
            //??????
            System.out.println(Thread.currentThread()+"---> " + pic.getTitle() + " " + pic.getUrl());
            HttpUtil.download(url, pic.getNumber(), f, isUseProxy());
        }
    }
}
